package com.cqcetbbs.smartlab.cqcetbbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.cqcetbbs.smartlab.cqcetbbs.R;
import com.cqcetbbs.smartlab.cqcetbbs.view.XCRoundRectImageView;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by user on 2017/9/28.
 * <p>
 * 校园页面
 */
public class CampusActivity extends AppCompatActivity implements View.OnClickListener {

    private static String NEWART_NEWS_PATH = "http://api.cqcetlab.com/Home/article/newart";


    private final static int SUCCESS = 1;
    private final static int FAILED = 2;


    ImageView cmapus_back;
    LinearLayout ll_club;
    LinearLayout ll_job;
    LinearLayout ll_life;
    LinearLayout ll_study;
    LinearLayout ll_trade;
    LinearLayout ll_notice;

    XCRoundRectImageView xcriv_new_one;
    XCRoundRectImageView xcriv_new_two;
    XCRoundRectImageView xcriv_hot_one;
    XCRoundRectImageView xcriv_hot_two;

    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case SUCCESS:

                    Toast.makeText(CampusActivity.this, "最新消息获取成功", Toast.LENGTH_SHORT).show();

                    break;
                case FAILED:
                    Toast.makeText(CampusActivity.this, "最新消息获取失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_campus);
        initView();

    }

    private void initView() {


        cmapus_back = (ImageView) findViewById(R.id.cmapus_back);
        ll_notice = (LinearLayout) findViewById(R.id.ll_notice);
        ll_club = (LinearLayout) findViewById(R.id.ll_club);
        ll_job = (LinearLayout) findViewById(R.id.ll_job);
        ll_life = (LinearLayout) findViewById(R.id.ll_life);
        ll_study = (LinearLayout) findViewById(R.id.ll_study);
        ll_trade = (LinearLayout) findViewById(R.id.ll_trade);
        xcriv_new_one = (XCRoundRectImageView) findViewById(R.id.xcriv_new_one);
        xcriv_new_two = (XCRoundRectImageView) findViewById(R.id.xcriv_new_two);

        xcriv_hot_one = (XCRoundRectImageView) findViewById(R.id.xcriv_hot_one);
        xcriv_hot_two = (XCRoundRectImageView) findViewById(R.id.xcriv_hot_two);


        ll_notice.setOnClickListener(this);
        ll_life.setOnClickListener(this);
        ll_club.setOnClickListener(this);
        ll_job.setOnClickListener(this);
        ll_study.setOnClickListener(this);
        ll_trade.setOnClickListener(this);
        xcriv_new_one.setOnClickListener(this);
        xcriv_new_two.setOnClickListener(this);
        xcriv_hot_two.setOnClickListener(this);
        xcriv_hot_one.setOnClickListener(this);
        cmapus_back.setOnClickListener(this);

//      xcriv_topic_one = (XCRoundRectImageView) findViewById(R.id.xcriv_topic_one);
//      xcriv_topic_two = (XCRoundRectImageView) findViewById(R.id.xcriv_topic_two);

        xcriv_new_one.setImageResource(R.drawable.tab1);

        getNews("4", "notice");

    }

    private void getNews(String num, String type) {

        final OkHttpClient client = new OkHttpClient();
        final String inner_num = num;
        final String inner_type = type;
        new Thread() {
            @Override
            public void run() {

                FormBody body = new FormBody.Builder()
                        .add("num", inner_num)
                        .add("notice", inner_type).build();

                Request request = new Request.Builder()
                        .url(NEWART_NEWS_PATH)
                        .post(body)
                        .build();
                Response response = null;
                try {
                    response = client.newCall(request).execute();

                    if (response.isSuccessful()) {
                        String result = response.body().string();
                        Log.e("news---", result);
                        Message msg = mhandler.obtainMessage(SUCCESS, result);
                        mhandler.sendMessage(msg);

                    } else {
                        Message msg = mhandler.obtainMessage(FAILED);
                        mhandler.sendMessage(msg);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }

        }.start();

    }


    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, NewsListActivity.class);
        switch (view.getId()) {

            case R.id.cmapus_back:
                finish();
                break;
            case R.id.ll_club:
                intent.putExtra("type", "club");
                startActivity(intent);
                break;
            case R.id.ll_job:
                intent.putExtra("type", "job");
                startActivity(intent);
                break;
            case R.id.ll_life:
                intent.putExtra("type", "life");
                startActivity(intent);
                break;
            case R.id.ll_study:
                intent.putExtra("type", "study");
                startActivity(intent);
                break;
            case R.id.ll_notice:
                intent.putExtra("type", "notice");
                startActivity(intent);
                break;
            case R.id.ll_trade:
                intent.putExtra("type", "trade");
                startActivity(intent);
                break;
            case R.id.xcriv_new_one:
                break;
            case R.id.xcriv_new_two:
                break;
            case R.id.xcriv_hot_one:
                break;
            case R.id.xcriv_hot_two:
                break;
        }
    }

}
